package email

import (
	"crypto/md5"
	"fmt"
	"math/rand"
	"net/smtp"
	"time"
)

// // 发QQ邮箱功能
//
//	func SendEmail(to string) {
//		auth := smtp.PlainAuth("", "445565722@qq.com", "ymuqrdulawaubjci", "smtp.qq.com")
//		body := []byte("From:445565722@qq.com\nTo:445565722@qq.com\nSubject:  你好,你是不是小学生")
//		smtp.SendMail("smtp.qq.com:587", auth, "445565722@qq.com", []string{"3537766467@qq.com"}, body)
//	}
// func SendQQEmail(to []string, title string, content string) {
// 	auth := smtp.PlainAuth("", "445565722@qq.com", "ymuqrdulawaubjci", "smtp.qq.com")
// 	realyBody := fmt.Sprintf("From:445565722@qq.com\nTo:%s\nSubject:%s\n%s", to, title, content)
// 	body := []byte(realyBody)
// 	smtp.SendMail("smtp.qq.com:587", auth, "445565722@qq.com", to, body)

func SendQQEmail(from string, to []string, title string, content string) error {

	auth := smtp.PlainAuth("", "445565722@qq.com", "555555", "smtp.qq.com")
	realyBody := fmt.Sprintf("From:%s\nTo:%s\nSubject:%s\n%s", from, to, title, content)
	body := []byte(realyBody)
	return smtp.SendMail("smtp.qq.com:587", auth, "445565722@qq.com", to, body)
}

// 给找回密码的人 发一个新的密码过去 。这个密码要随机性

// 随机密码生成

// 随机密码生成器
func SuiJiMima(changdu int) (mingwen string, miwen string) {
	var allChar string = "ABCDEFG1234567890"
	var mima string
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < changdu; i++ {
		current := r.Intn(len(allChar) - 1)
		zimu := allChar[current]
		mima = mima + string(zimu)
	}
	miwenBytes := md5.Sum([]byte(mima))
	return mima, fmt.Sprintf("%x", miwenBytes)
}

// func ResetPswd() {
// 	var sn string
// 	fmt.Println("请输入你要充值密码的人学号")
// 	fmt.Scanln(&sn)
// 	var student database.Classmates
// 	database.DB.Raw("select * from classmates.classmates where sn=?", sn).Scan(&student)
// 	mingwen, miwen := SuiJiMima(6)
// 	student.Pswd = miwen
// 	database.DB.Save(student)
// 	SendQQEmail("445565722@qq.com", []string{student.Email}, "你的新密码重置成功，请查收", mingwen)
// 	fmt.Println("你的新密码是:", mingwen)
// }
